/*************************************************************************
 * Copyright 2008 David Almilli
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *      
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 **************************************************************************/
package com.almilli.catnip.web;

import com.tivo.hme.interfaces.IHttpRequest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * The <code>HmeHttpRequest</code> class is a wrapper for the servlet request api.
 * @author s2kdave
 */
public class HmeHttpRequest implements IHttpRequest {
    
    private String uri;
    private HttpServletRequest request;
    private HttpServletResponse response;
    
    public HmeHttpRequest(String uri, HttpServletRequest request, HttpServletResponse response) {
        this.uri = uri;
        this.request = request;
        this.response = response;
    }

    public void addHeader(String header, String value) throws IOException {
        response.addHeader(header, value);
    }

    public void close() throws IOException {
        response.getOutputStream().close();
    }

    public String get(String header) {
        if ("http-method".equals(header)) {
            return request.getMethod();
        }
        return request.getHeader(header);
    }

    public InputStream getInputStream() throws IOException {
        return request.getInputStream();
    }

    public String getMethod() {
        return request.getMethod();
    }

    public OutputStream getOutputStream() throws IOException {
        return response.getOutputStream();
    }

    public OutputStream getOutputStream(long length) throws IOException {
        return response.getOutputStream();
    }

    public boolean getReplied() {
        return response.isCommitted();
    }

    public String getURI() {
        return uri;
    }

    @SuppressWarnings("deprecation")
    public void reply(int code, String message) throws IOException {
        response.setStatus(code, message);
    }

}
